package ren.kane.phoenix.some.async;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @author renrk(1275) on 2018-03-27 2:33 PM
 **/
@RestController
@RequestMapping("/phoenix/async")
public class AsyncTaskController {

    @Autowired
    private AsyncTask asyncTask;

    @GetMapping
    public String doTask() throws InterruptedException {
        long currentTimeMillis = System.currentTimeMillis();
        Future<String> task1 = asyncTask.task1();
        Future<String> task2 = asyncTask.task2();
        Future<String> task3 = asyncTask.task3();
        for (; ; ) {
            if (task1.isDone() && task2.isDone() && task3.isDone()) {
                break;
            }
            Thread.sleep(1000);
        }
        long currentTimeMillis1 = System.currentTimeMillis();
        return "task任务总耗时：" + (currentTimeMillis1 - currentTimeMillis) + "ms";
    }
}
